﻿using T4UtilityBelt.Writers;
using System.Linq;

namespace T4UtilityBelt.UxDesigns.Controls.Renderers.Android
{
    public class ListBoxRenderer : ControlRenderer<ListBoxControl, IListBoxRenderer>, IListBoxRenderer
    {
        protected override void OnRenderUiXmlBegin(ListBoxControl control, CodeWriter writer)
        {
            //        <Mvx.MvxBindableListView
            //  android:layout_width="fill_parent"
            //  android:layout_height="fill_parent"
            //  android:orientation="vertical"
            //  local:MvxItemTemplate="@layout/speaker_listitem"
            //  local:MvxBind="{'ItemsSource':{'Path':'Data'},'ItemClick':{'Path':'SpeakerSelectedCommand'}}"
            //>

            writer.WriteBeginTag("Mvx.MvxBindableListView");
            RenderFirstElementAttributes(writer);
            RenderControlAttributes(control, writer);

            // todo: use the correct Templates
            ItemTemplateControl itemTemplate = GetFirstItemTemplate(control);
            if (itemTemplate != null)
            {
                writer.WriteAttributeFormat("local:MvxItemTemplate", "@layout/{0}", itemTemplate.Name.ToLower());
            }
            writer.AddBindingToList("ItemsSource", control.ItemsSource);
            writer.AddBindingToList("ItemClick", control.ItemSelectedCommand);

            writer.RenderBindingsAttribute();

            //writer.WriteAttributeFormat("local:MvxBind", "{{'ItemsSource':{{'Path':'{0}'}},'ItemClick':{{'Path':'{1}'}}}}", control.ItemsSource.SourcePropertyPath, control.ItemSelectedCommand);

            writer.WriteFullEnd();
        }

        private static ItemTemplateControl GetFirstItemTemplate(ListBoxControl control)
        {
            return control.Templates.FirstOrDefault();
        }

        protected override void OnRenderUiXmlContent(ListBoxControl control, CodeWriter writer)
        {

        }
    }
}